let cols4UserList = [
    { type: 'checkbox', fixed: 'left' },
    { field: 'id', fixed: 'left', width: 80, title: 'ID', sort: true, totalRowText: '合计：' },
    { field: 'username', width: 80, title: '用户' },
    { field: 'email', title: '邮箱 <i class="layui-icon layui-icon-tips layui-font-14" lay-event="email-tips" title="该字段开启了编辑功能" style="margin-left: 5px;"></i>', fieldTitle: '邮箱', hide: 0, width: 150, edit: 'text' },
    { field: 'sex', width: 80, title: '性别', sort: true },
    { field: 'sign', title: '签名', edit: 'textarea', minWidth: 260, expandedWidth: 260},
    { field: 'experience', width: 100, title: '积分', sort: true, totalRow: '{{= d.TOTAL_NUMS }} 😊' },
    { field: 'checkin', title: '打卡', width: 100, sort: true, totalRow: '{{= parseInt(d.TOTAL_NUMS) }} 次' },
    { field: 'ip', title: 'IP', width: 120 },
    { field: 'joinTime', title: '加入时间', width: 120 },
    { fixed: 'right', title: '操作', width: 134, minWidth: 125, toolbar: '#barDemo' }
];

// 初始化表格
function initTable(data,colsInit) {
    layui.use(['table'], function () {
        var table = layui.table;
        // 渲染表格
        table.render({
            elem: '#userTable',
            data: data,
            cols: [colsInit],
            page: true,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            limits: [10, 20, 30, 40, 50],
            limit: 10,
            height: 'full-220'
        });

        // 监听工具条事件
        table.on('tool(userTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.prompt({
                    formType: 2,
                    value: data.email
                }, function (value, index) {
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });

        // 监听头部工具栏事件
        table.on('toolbar(userTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;
            };
        });
    });
}

let request = $.ajax({
    url: "/admin/loadUserList",
    method: "GET",
    dataType: "json"
});
request.done(function( data ) {
    if(data.code===901){ // 登录超时
        popup.warning(data.info, function () {
             console.log(data.info);
        });
        setTimeout(function () {
            location.href = "/login";
        }, 1500);
    }
    console.log(data);

    // 成功获取数据
    if(data.code===200){
        tableData={
            data:request.data.data.list,
            count:request.data.data.totalCount
        };
    }
});

request.fail(function( jqXHR, textStatus ) {
  console.error(jqXHR,textStatus);
});